<?php
/**
 * Created by PhpStorm.
 * User: 17208
 * Date: 2020/10/15
 * Time: 14:17
 */

namespace app\server;


use app\server\taobao\ErrorCode;
use app\utility\TaoBaoApp;
use extend\Config;
use extend\Common;
use extend\Log;

class ServerTaoBao
{
    /**
     * 解析淘口令并转为高佣链接
     * @param  $content String
     * @return array|object
     */
    public static function parseTkl2Url($content){
        try{
            // 淘口令解析
            $pid_data = TaoBaoApp::getPid();
            $app = TaoBaoApp::get();
            $params = [
                'tkl'=>$content
            ];
            $data = $app->tkl->Tkljm($params);
            $itemName = $data['content'];

            // 商品ID解析
            $itemid = Common::parseUrlItemId($data['url']);
            if(!$itemid)
                return [1,ErrorCode::$errorMsg[ErrorCode::$passwordError]];

            // 优惠卷查询
            $params = [
                "itemid"=>$itemid,
                "site_id"=>$pid_data['site_id'],
                "adzone_id"=>$pid_data['adzone_id'],
            ];
            $data = $app->tkl->TbkPrivilegeGet($params);
            $data = $data['result']['data'];

            //是否有优惠卷
            if(!$data['coupon_info']){
                return [2,$itemName];
            }
            //淘口令生成
            $params = [
                'text'=>$data['coupon_info'],
                'url'=>$data['coupon_click_url']
            ];
            $data = $app->tpwd->create($params);
            return [0,$data['data']['model']];
        }catch (\Exception $e){
            Log::getInstance('taobao')->error('淘口令转高佣链接错误：'.$e->getMessage());
            return [-1,$e->getMessage()];
        }
    }
}